let fetch = require("../../utils/fetch");
let mta = require('../../utils/mta_analysis.js');
let util = require("../../utils/util");

let app = getApp();
Page({
    data: {
        showFaceToast: false,
        orderCode: '',
        discountPrice: '',
        orderPrice: '',
        imageUrl: app.globalData.imageUrl,
        wechatStatus: false,
        imageUrl1: app.globalData.imageUrl,
        reserveCode: '',
        turnUrl: '',
        type: ''
    },
    onLoad: function(options) {
        mta.Page.init()
        // 生命周期函数--监听页面加载
        console.log(options)
        if (options.from) {
            this.setData({
                orderCode: options.orderCode,
                discountPrice: options.discountPrice,
                orderPrice: options.orderPrice,
                from: options.from

            });
        } else {
            this.setData({
                orderCode: options.orderCode,
                discountPrice: options.discountPrice,
                orderPrice: options.orderPrice,
                from: null

            });
        }
        console.log("****" + options.type)
        if (options.type == 0) {
            this.setData({
                turnUrl: '/pages/detail/detail?code=' + options.orderCode
            })
        } else if (options.type == 1) {
            this.setData({
                turnUrl: '/pages/reserveOrder/reserveOrder?code=' + options.orderCode
            })
        }

        this.popCoupons(options.orderCode)
        // this.getReserveCode();
    },
    goToOrder: function() {
        mta.Event.stat("smg_btdingdanchakan", {})
    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
    onShow: function() {
        // 生命周期函数--监听页面显示 
        fetch.loadingText()
        this.isShowUploadImg();
        const self = this;
        fetch
            .freePayStatus()
            .then(response => {
                fetch.hideLoadingText();
                const res = response.data;
                // 0：未签约  1：已签约
                if (res.code == 200 && res.obj.WX_APPLET == 1) {
                    this.setData({
                        wechatStatus: true
                    })

                } else {
                    this.setData({
                        wechatStatus: false
                    })
                }

            })
            .catch(error => {

                this.setData({
                    wechatStatus: false
                })
                fetch.hideLoadingText();
                fetch.showMyToast(self, error.data.des)
            })
        this.upgrade()

    },

    isShowUploadImg: function() {
        fetch.hasUploadImg().then((response) => {
            const res = response.data;
            if (res.code == 200 && res.obj) {
                this.setData({
                    showFaceToast: false
                })

            }
        }).catch((error) => {
            console.log("error", error)
            if (error.data.code == 404 && this.judgeFaceShowFlag(24)) {
                this.setData({
                    showFaceToast: true
                })

            }
            //     if(this.judgeFaceShowFlag(0.05)) {
            //         this.setData({
            //             showFaceToast:true
            //         }) 
            //    }

        })
    },
    close: function() {
        this.setData({
            showFaceToast: false
        })
        this.setFaceShowFlag()

    },
    setFaceShowFlag: function() {
        wx.setStorage({
            key: "faceShowFlag",
            data: +new Date()
        })
    },
    judgeFaceShowFlag: function(hours) {
        const time1 = wx.getStorageSync('faceShowFlag');
        if (!time1) {
            return true
        }
        const now = +new Date();
        console.log(now, time1, now - time1, hours * 60 * 60 * 1000);
        const pass = hours * 60 * 60 * 1000
        const flag = now - time1 - pass >= 0 ? true : false;
        if (flag) {
            return true
        } else {
            return false
        }
    },
    toFace: function() {
        mta.Event.stat("smg_btdingdanrenlian", {})
        this.setData({
            showFaceToast: false
        })
        this.setFaceShowFlag();
        wx.redirectTo({
            url: '../face/face'
        })
    },
    goscan: function() {
        mta.Event.stat("smg_btdingdanjixu", {})
        if (this.data.from) {
            wx.redirectTo({
                url: '../scan/scan',
            })
        } else {
            wx.navigateBack({
                delta: 1
            })
        }

    },
    onHide: function() {
        // 生命周期函数--监听页面隐藏
        console.log("onHide")
    },
    onUnload: function() {},
    popCoupons: function(orderCode) {

        if (!wx.getStorageSync('firstOrder')) {
            //新手券
            fetch.firstOrder(orderCode).then(response => {
                wx.setStorageSync('firstOrder', '1')
                if (response.data.obj && response.data.obj.length > 0) {
                    this.setData({
                        my_pop: 'my_pop',
                        popData: {
                            type: 'order',
                            imageUrl: getApp().globalData.imageUrl,
                            obj: response.data.obj.slice(0, 3)
                        }
                    })
                }

            })
        }

    },
    upgrade: function() {
        fetch.upgradeVouchers().then(response => {
            console.log('upgradeVouchers', response)
            if (response.data.obj && response.data.obj.length > 0) {
                this.setData({
                    my_pop: 'my_pop',
                    popData: {
                        type: 'upgrade',
                        imageUrl: getApp().globalData.imageUrl,
                        obj: response.data.obj.slice(0, 3)
                    }
                })
            }
        }).catch(error => {
            // console.log('error',error)
        })
    },
    closePop: function() {
        util.popClose(this)
    },
    go(e) {
        this.closePop()
        util.popGo(e)
    },
    getReserveCode: function() {

        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/reserve/getReserveMealCode.do',
            data: {
                orderCode: this.data.orderCode,
            },
            header: getApp().globalData.header,
            success: function(res) {
                if (res.data.code == 200) {
                    _self.setData({
                        reserveCode: res.data.obj.reserveMealCode
                    })

                }
                console.log(res)
            }
        })

    }

})